package com.yanqu.road.server.cmd.orewar.cross;

import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.crossorewar.CrossUserOrePit;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.orewar.UserOrePit;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.OreWarModule;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.pb.OreWarPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_ORE_WAR_ROBPIT, desc = "抢夺矿坑情况跨服返回")
public class CrossRobPitRespCmd extends AbstractCommand {


	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		OreWarProto.CrossOrePitRobRespMsg crossRespMsg = OreWarProto.CrossOrePitRobRespMsg.parseFrom(packet.getBytes());
		ret = crossRespMsg.getRet();

		OreWarProto.OrePitRobRespMsg.Builder resMsg = OreWarProto.OrePitRobRespMsg.newBuilder();
		resMsg.setActivityId(crossRespMsg.getActivityId());
		resMsg.setRet(ret);
		if(ret == 0){
			List<Integer> patronIdList = PatronsMgr.parsePatronInfoStrToIdList(crossRespMsg.getPatronsInfoStr());
			//胜利了，发奖励
			if(crossRespMsg.getWinUserId() == player.getUserId()){
				CrossUserOrePit crossUserOrePit = OreWarPb.parseCrossUserOrePit(crossRespMsg.getCrossOrePit());
				OreWarMgr.addCrossOreWarReward(resMsg.getActivityId(),player,crossRespMsg.getWinReward(), crossRespMsg.getCrossOrePit().getPitId(),patronIdList,crossUserOrePit,crossRespMsg.getGroupId());
				resMsg.setOrePit(OreWarPb.parseOrePitTempMsg(cross2ServerUserOrePit(crossUserOrePit)));
			}
			// 成功才发
			SeasonTaskConditionData data = new SeasonTaskConditionData(BigInteger.valueOf((long) eActivityType.OreWarActivity.getValue()), BigInteger.ONE);
			player.notifyListener(eGamePlayerEventType.SaltCostStaminaTask.getValue(), data);
			resMsg.setWinUserId(crossRespMsg.getWinUserId());
			resMsg.addAllPatronsIdList(patronIdList);
			resMsg.setBattleDetailList(crossRespMsg.getBattleDetailList());
			resMsg.addAllEnemyPatronsList(crossRespMsg.getEnemyPatronsListList());
			resMsg.setWinReward(crossRespMsg.getWinReward());
		}else{//报错了无事发生
			/*OreWarProto.CrossGetAllUserPitMsg.Builder reqMsg = OreWarProto.CrossGetAllUserPitMsg.newBuilder();
			reqMsg.setActivityId(crossRespMsg.getActivityId());
			reqMsg.setPitId(crossRespMsg.getCrossOrePit().getPitId());
			YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_OREWAR_GET_ALL_USERPIT, reqMsg);
			GamePlayerMgr.sendChannelPacket(0, pbMsg);*/
			//报错没有开打退换hp
			if(GameErrorCode.E_ORE_WAR_ORE_PIT_OWNER_CHANGE == ret){
				/*CrossUserOrePit crossUserOrePit = OreWarPb.parseCrossUserOrePit(crossRespMsg.getCrossOrePit());
				resMsg.setOrePit(OreWarPb.parseOrePitTempMsg(cross2ServerUserOrePit(crossUserOrePit)));*/
				resMsg.setRet(0);
				resMsg.setWinUserId(0);
			}
			player.getModule(OreWarModule.class).addHp(1,crossRespMsg.getActivityId());
		}
		player.sendPacket(Protocol.U_ORE_WAR_ROB_PIT,resMsg);
	}

	@Override
	public void sendData(GamePlayer player) {

	}

	//把跨服的格式转换成本服的下发下去
	private UserOrePit cross2ServerUserOrePit(CrossUserOrePit crossUserOrePit){
		UserOrePit temp = new UserOrePit();
		temp.setActivityId(crossUserOrePit.getActivityId());
		temp.setPitId(crossUserOrePit.getPitId());
		temp.setStationId(crossUserOrePit.getStationId());
		temp.setUserId(crossUserOrePit.getUserId());
		temp.setPatronsIdList(PatronsMgr.parsePatronInfoStrToIdList(crossUserOrePit.getPatronsInfoStr()));
		temp.setJoinTime(crossUserOrePit.getJoinTime());
		temp.setLastGetRewardTime(crossUserOrePit.getLastGetRewardTime());
		temp.setNickName(crossUserOrePit.getNickName());
		temp.setDecoration(crossUserOrePit.getUserBaseInfo().getDecoration());
		temp.setUnionName(crossUserOrePit.getUserBaseInfo().getUnionName());
		temp.setBlowout(crossUserOrePit.isBlowout());
		return temp;
	}
}
